package com.ckjw.gobang.view;

import java.awt.Event;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;

import com.ckjw.gobang.connect.UserClientService;
import com.ckjw.gobang.data.GameCenter;
import com.ckjw.gobang.data.Player;
import com.ckjw.gobang.utils.Path;

//import com.bzchao.gobang.view.MyMenuBar;

/**
 * 主界面UI
 * 
 * @author lck and djw
 * @Date 2022年6月2日-下午10:14:00
 */
public class MainInterface extends JFrame {
	// TODO
	private static final long serialVersionUID = 1L;
	// 版本 开发时间 版权
	public static final String VERSION = "1.1.1";
	public static final String BUILD_TIME = "2022.6.2";
	public static final String COPYRIGHT = "自主开发，拥有所有版权";
	// 窗体大小
	private int width = 730;// 730
	private int height = 800;// 760
	// 创建一个本类的对象
	public static MainInterface mainInterface;

	// 定义一个右键菜单，用于设置程序的外观风格
	JPopupMenu pop = new JPopupMenu();

	// 定义一个ButtongGroup对象，用于组合风格按钮，形成单选
	ButtonGroup flavorGroup = new ButtonGroup();

	// 定义五个单选按钮菜单项，用于设置程序风格
	JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal 风格", true);
	JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus 风格", true);
	JRadioButtonMenuItem classicItem = new JRadioButtonMenuItem("Motif 风格", true);
	JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("synth 风格", true);

	// 默认构造器直接执行窗口创建的方法体
	public MainInterface() {
		this.setLayout(null);
		basicSettings();
		addwight();
		stylechange();
		this.setVisible(true);

	}

	/**
	 * 窗口基本设置
	 */
	public void basicSettings() {
		mainInterface = this;
		// TODO 用设置ui框架设置字体
		UIManager.put("Label.font", new Font("宋体", Font.BOLD, 15));
		UIManager.put("Button.font", new Font("宋体", Font.PLAIN, 20));
		// 窗口基本设置
		this.setTitle(" 五子棋  " + BUILD_TIME);
		this.setSize(width, height);
		this.setResizable(false); // 设置是否可调整大小

		// 设置窗口在屏幕中央出现
		int sWidth = Toolkit.getDefaultToolkit().getScreenSize().width;// 获取当前屏幕宽度
		int sHeight = Toolkit.getDefaultToolkit().getScreenSize().height;// 获取当前屏幕高度
		this.setLocation((sWidth - width) / 2, (sHeight - height) / 2);
		// 设置窗口关闭，虽然swing能点x就退出，但实际上程序并没有结束运行。
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				close();
			}
		});
		// 设置app启动图标
		// 获取src绝对路径，使在任何系统都可以找到该路径
		try {
			// System.out.println(path);
			ImageIcon imgIcon = new ImageIcon(Path.getsrcurl(this, "images/maininterface/goBang.png"));
			this.setIconImage(imgIcon.getImage());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("图表设置失败");
		}
	}

	/**
	 * 组合右键切换风格按钮并设置监听器
	 */
	public void stylechange() {
		flavorGroup.add(metalItem);
		flavorGroup.add(nimbusItem);
		flavorGroup.add(classicItem);
		flavorGroup.add(motifItem);

		// 给5个风格菜单创建事件监听器
		ActionListener flavorLister = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String command = e.getActionCommand();
				try {
					changeFlavor(command);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		};

		// 为5个风格菜单项注册监听器
		metalItem.addActionListener(flavorLister);
		nimbusItem.addActionListener(flavorLister);
		classicItem.addActionListener(flavorLister);
		motifItem.addActionListener(flavorLister);

		pop.add(metalItem);
		pop.add(nimbusItem);
		pop.add(classicItem);
		pop.add(motifItem);

		// 调用ta组件的setComponentPopupMenu即可设置右键菜单，无需使用事件
		ControlPanel.jtext.setComponentPopupMenu(pop);
	}

	/**
	 * 定义一个方法，用于改变界面风格
	 * 
	 * @param command
	 * @throws Exception
	 */
	private void changeFlavor(String command) throws Exception {
		switch (command) {
		case "Metal 风格":
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
			break;
		case "Nimbus 风格":
			UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
			break;
		case "Motif 风格":
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
			break;
		case "synth 风格":
			UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");
			break;
		}

		// 更新f窗口内顶级容器以及所有组件的UI
		SwingUtilities.updateComponentTreeUI(mainInterface.getContentPane());
		// 更新mb菜单条及每部所有组件UI
		SwingUtilities.updateComponentTreeUI(MyMenuBar.myMenuBar);
		// 更新右键菜单及内部所有菜单项的UI
		SwingUtilities.updateComponentTreeUI(pop);
	}

	/**
	 * 添加菜单和其它组件
	 */
	private void addwight() {
		int x = 5;
		int y = 2;
		int mWidth = width / 4 * 3;
		int mHeight = height / 5;

		this.setJMenuBar(new MyMenuBar());
		// 添加之前已经组装好的面板
		mainInterface.add(new UserPanel());// 注意此种写法new user相当于把对象赋给了userpanel
		UserPanel.userPanel.setBounds(x, y, width, mHeight);// 设置面板的位置和大小宽度占3/4 高度占1/5s
		// 登陆后的面板组件先隐藏，登陆成功显示
		mainInterface.add(new UserPanel2());
		UserPanel2.userPanel.setBounds(x, y, width, mHeight);
		mainInterface.add(new ChessBroad());
		ChessBroad.myBroad2.setBounds(x, mHeight + 2 * y, mWidth, height - mHeight - 2 * y);
		mainInterface.add(new ControlPanel());
		ControlPanel.my.setBounds(mWidth + 2 * x, mHeight + 2 * y, width - mWidth - 2 * x, height - mHeight);

	}

	/**
	 * 显示各分部面板
	 * 
	 * @throws IOException
	 */
	public static void init() throws IOException {
		GameCenter.reStart();
		UserPanel.init();
		ChessBroad.myBroad2.init();
		mainInterface.repaint();// 猜测，不执行它，用户面板可能有时加载不出来
	}

	/**
	 * 关闭窗口
	 */
	public static void close() {
		if (GameCenter.getMode() == GameCenter.MODE_ONLINE) {
			int i = JOptionPane.showConfirmDialog(mainInterface, "检测到您正在网络对战，确定认输退出吗？", "提示信息",
					JOptionPane.OK_CANCEL_OPTION);
			if (i == JOptionPane.YES_OPTION) {
				UserClientService.setover();
				if (Player.onlineuser != null) {
					new UserClientService().logout();// new的话会出现新的socket故socket应设置static
				}
				System.exit(0);
			} else {
				// 不执行操作
				mainInterface.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// 0
				// System.out.println("1");
			}

		} else {
			int i = JOptionPane.showConfirmDialog(mainInterface, "确定退出五子棋吗?", "正在退出五子棋···",
					JOptionPane.OK_CANCEL_OPTION);
			if (i == JOptionPane.YES_OPTION) {
				// mainInterface.setDefaultCloseOperation(EXIT_ON_CLOSE);//3 这种方式只适合鼠标点击的情况
				// 如果登陆了网络用户给服务端发送退出消息
				if (Player.onlineuser != null) {
					new UserClientService().logout();// new的话会出现新的socket故socket应设置static
				}
				System.exit(0);
			} else {
				// 不执行操作
				mainInterface.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// 0
				// System.out.println("1");
			}

		}

	}

}
